package bizlogic;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import model.PublicHolidayDTO;

import dao.DAOException;
import dao.DAOfactory;
import dao.PublicHolidayDAO;
import dao.mysql.DAOFactoryImpl;

public class CalculateLeaveDays {

	public CalculateLeaveDays() {
	}

	public int calculate(Date sd, Date ed) {
		int days = 0;
		Calendar st = new GregorianCalendar();
		st.setTime(sd);
		long d1 = st.getTimeInMillis();

		Calendar en = new GregorianCalendar();
		st.setTime(ed);
		long d2 = en.getTimeInMillis();

		days = (int) ((d2 - d1) / (24 * 60 * 60 * 1000));

		return days;

	}

	public int calculate2(Date sd, Date ed) {
		int days = 0;
		Calendar st = new GregorianCalendar();
		st.setTime(sd);
		long d1 = st.getTimeInMillis();

		Calendar en = new GregorianCalendar();
		en.setTime(ed);
		long d2 = en.getTimeInMillis();

		days = (int) ((d2 - d1) / (24 * 60 * 60 * 1000));

		DAOfactory f = new DAOFactoryImpl();
		PublicHolidayDAO pbDAO = f.getPublicHolidayDAO();
		ArrayList<PublicHolidayDTO> pbList = new ArrayList<PublicHolidayDTO>();
		try {
			pbList = pbDAO.findAllPublicHoliday();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		while (st.before(en)) {
			if (st.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
					|| st.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				days--;
			}

			for (PublicHolidayDTO pbhol : pbList) {

				if (st.equals(pbhol.getDate())) {
					days--;
				}

				st.add(Calendar.DAY_OF_MONTH, 1);
			}
		}
		return days;
	}

}
